package org.example.score.service.impl;

import com.google.gson.Gson;
import org.example.score.mapper.WeightMapper;
import org.example.score.service.WeightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WeightServiceImpl implements WeightService {

    @Autowired
    private WeightMapper weightMapper;

    @Override
    public String getWeight() {

        List<Map<String, Object>> weightList = weightMapper.getWeight();
        List<Map<String, Object>> columnNames = weightMapper.getColumnNames();
        Map<String, Object> data = new HashMap<>();
        data.put("weight", weightList);
        data.put("column", columnNames);

        Gson gson = new Gson();
        String json = gson.toJson(data);
        System.out.println("获取权重了=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
        System.out.println(json);
        System.out.println("=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
        return json;
    }

    @Override
    public String setWeight(List<Map<String, Object>> Weight) {
        if (Weight == null || Weight.isEmpty()) {
            throw new RuntimeException("修改权重参数有问题");
        }
        System.out.println("修改权重开始： " + Weight.size());
        for (Map<String, Object> map : Weight) {

            String zbh = map.get("zbh").toString();
            Double zbqz = (Double) map.get("zbqz");
            String qzlx = map.get("qzlx").toString();

            weightMapper.update1(zbqz, zbh, qzlx);
        }

        return "ok" + Weight.size();
    }

    public static int COUNT = 37;

    @Override
    public Map<Integer, Double> getweightNumber(String major) {
        List<Map<String, Object>> weights = weightMapper.getWeight();

        Map<Integer, Double> map = new HashMap<>();
        if (weights.size() == COUNT) {
            for (Map<String, Object> item : weights) {
                int key = Integer.parseInt(item.get("zbh").toString());
                Double value = Double.parseDouble(item.get("zbqz").toString());
                map.put(key, value);
            }
            System.out.println("当前权重表（获取，格式化）：" + map.toString());
            return map;
        } else {
            throw new RuntimeException("计算需要的权重表异常");
        }
    }

}
